package com.hxgz.steel.controller.backend;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.hxgz.steel.common.Const;
import com.hxgz.steel.common.ResponseCode;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.domain.CompanyCert;
import com.hxgz.steel.domain.Page;
import com.hxgz.steel.domain.User;
import com.hxgz.steel.service.BackendUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(value = "BackendUserController", description = "后台：用户管理")
@RestController
@RequestMapping("BackendUser")
public class BackendUserController {

    @Autowired
    BackendUserService backendUserService;

    @ApiOperation("个人管理列表查询")
    @RequestMapping(value = "checkUser.do", method = RequestMethod.POST)
    public ServerResponse<PageInfo<User>> checkUser(
            @ApiParam("认证 0未认证 1待审核 2审核未通过 3审核已通过") @RequestParam(required = false) Integer status,
            @ApiParam("提交开始时间") @RequestParam(required = false) String startDate,
            @ApiParam("提交结束时间") @RequestParam(required = false) String endDate,
            @ApiParam("账号") @RequestParam(required = false) String username, Page page,
            HttpServletRequest httpServletRequest) {
        User user = Const.checkBackUserLogin(httpServletRequest);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
                    ResponseCode.NEED_LOGIN.getDesc());
        }
        HashMap<Object, Object> map = Maps.newHashMap();
        map.put("status", status);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("username", username);
        return backendUserService.checkUser(map, page);
    }

    @ApiOperation("个人管理列表详情查询")
    @RequestMapping(value = "checkUserId.do", method = RequestMethod.POST)
    public ServerResponse<CompanyCert> checkUserId(@RequestParam String id,
                                                   HttpServletRequest httpServletRequest) {
        User user = Const.checkBackUserLogin(httpServletRequest);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
                    ResponseCode.NEED_LOGIN.getDesc());
        }
        return backendUserService.checkUserId(id);
    }


    @ApiOperation("企业认证列表查询")
    @RequestMapping(value = "checkCompanyCert.do", method = RequestMethod.POST)
    public ServerResponse<List<CompanyCert>> checkCompanyCert(
            @ApiParam("审核状态 0待审核 1审核未通过 2审核已通过") @RequestParam(required = false) Integer status,
            @ApiParam("开始日期") @RequestParam(required = false) String startDate,
            @ApiParam("结束日期") @RequestParam(required = false) String endDate,
            @ApiParam("账号") @RequestParam(required = false) String username,
            @ApiParam("公司名称") @RequestParam(required = false) String companyName, Page page,
            HttpServletRequest httpServletRequest) {
        User user = Const.checkBackUserLogin(httpServletRequest);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
                    ResponseCode.NEED_LOGIN.getDesc());
        }
        HashMap<Object, Object> map = Maps.newHashMap();
        map.put("status", status);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("userName", username);
        map.put("companyName", companyName);
        return backendUserService.checkCompanyCert(map, page);
    }

    @ApiOperation("企业认证id查询或修改审核状态")
    @RequestMapping(value = "checkCompanyCertId.do", method = RequestMethod.POST)
    public ServerResponse<CompanyCert> checkCompanyCertId(@RequestParam String id,
                                                          @ApiParam("审核状态 0通过 1不通过") @RequestParam(required = false) Integer status,
                                                          HttpServletRequest httpServletRequest) {
        User user = Const.checkBackUserLogin(httpServletRequest);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
                    ResponseCode.NEED_LOGIN.getDesc());
        }
        try {
            ServerResponse serverResponse = backendUserService.checkCompanyCertId(id, status);
            return serverResponse;
        } catch (Exception e) {
            return ServerResponse.createByErrorMessage("审核失败");
        }
    }
}
